# -*- coding:utf-8 -*-

# csv文件
"""
from pathlib import Path

p = Path('./abc.csv')
parent = p.parent
if not parent.exists():
    parent.mkdir(parents=True)

csv_body = '''\
id,name,age,comment
1,zs,25,"I'm 25"
2,ls,20,"this a a test string"
3,ww,23,"你好,
计算机
"
'''
p.write_text(csv_body)
"""

# csv模块
"""
import csv,pathlib
p = pathlib.Path('./test.csv')
with open(str(p)) as f:
    reader = csv.reader(f)
    print(next(reader))
    print(next(reader))

rows = [
    [4,'tom',22,'tom']
]
row = rows[0]
with open(str(p),'a') as f:
    writer = csv.writer(f)
    writer.writerow(row)
    writer.writerows(rows)
"""

from configparser import ConfigParser

filename = 'test.ini'
newfilename = 'mysql.ini'

cfg = ConfigParser()
cfg.read(filename)
print(cfg.sections)
print(cfg.sections)
print(cfg.has_section('client'))

print(cfg.items('mysqld'))
for k,v in cfg.items():
    print(k,type(v))
    print(k,cfg.items(k))
    

tmp = cfg.get('mysqld','port')
print(type(tmp),tmp)
print(cfg.get('mysqld','a'))
print(cfg.get('mysqld','first',fallback='python'))

tmp = cfg.getint('mysqld','port')
print(type(tmp),tmp)

if cfg.has_section('test'):
    cfg.remove_section('test')

cfg.add_section('test')
cfg.set('test','test1','1')
cfg.set('test','test2','2')

with open(newfilename,'w') as f:
    cfg.write(f)

print(cfg.getint('test','test2'))

cfg.remove_option('test','test2')

# 字典操作
cfg['test']['x'] = '100'
cfg['test2'] = {'test2': '100'}

print('x' in cfg['test'])
print('x' in cfg['test2'])

print(cfg._dict)  # 返回默认字典类型,内部使用有序字典.

# 修改后在写入
with open(newfilename,'w') as f:
    cfg.write(f)
    